package bsk.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import komunikaty.Kandydaci;

import bsk.Connect;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Glosowanie extends JPanel {
	private final ButtonGroup buttonGroup = new ButtonGroup();
	JRadioButton[] rdbtnImieNazwisko = new JRadioButton[20];
	
	private JLabel lblsub;
	private JPanel panelMenu;
	
	public Glosowanie(JLabel lbl, JPanel panelMenu) {
		this.lblsub = lbl;
		this.panelMenu = panelMenu;
		
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setLayout(null);
		setSize(477, 361);
		
		JLabel lbGlosowanie = new JLabel("G\u0142osowanie");
		lbGlosowanie.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lbGlosowanie.setBounds(10, 11, 75, 18);
		add(lbGlosowanie);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setAlignmentY(Component.TOP_ALIGNMENT);
		scrollPane.setBounds(0, 40, 477, 261);
		add(scrollPane);
		
		JPanel panel = new JPanel();
		panel.setAlignmentY(Component.TOP_ALIGNMENT);
		panel.setPreferredSize(new Dimension(200, 300));
		scrollPane.setViewportView(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 5));
		
		final JButton btnGlosuj = new JButton("G\u0142osuj");
		btnGlosuj.setEnabled(false);
		btnGlosuj.addActionListener(alGlosuj);
		btnGlosuj.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnGlosuj.setBounds(355, 312, 87, 35);
		add(btnGlosuj);
		
		
		Connect con = new Connect("servers.jks", "098765", false, true, false, false);
		//con.setClientData("dsad", "dsada", "231312");//poprawi�!!!!!!!!!! poprawi� connect
		con.run();
		

	    
		Kandydaci[] listaKandydatow = con.listaKand;
		int i = 0;
		for(Kandydaci ka : listaKandydatow) {
			rdbtnImieNazwisko[i] = new JRadioButton(ka.StringGlosowanie()+"\t");
			//rdbtnImieNazwisko[i].setName(" "+ka.getImie()+" "+ka.getNazwisko());
			rdbtnImieNazwisko[i].setActionCommand(""+ka.getNR());
			rdbtnImieNazwisko[i].setFont(new Font("Tahoma", Font.PLAIN, 13));
			rdbtnImieNazwisko[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btnGlosuj.setEnabled(true);
				}
			});
			buttonGroup.add(rdbtnImieNazwisko[i]);
			//rdbtnImieNazwisko[i].setHorizontalTextPosition(SwingConstants.LEADING);
			rdbtnImieNazwisko[i].setPreferredSize(new Dimension(225, 35));
			panel.add(rdbtnImieNazwisko[i]);
			++i;
		}

	}
	
ActionListener alGlosuj = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("--------------------------------------------");
			System.out.println( buttonGroup.getSelection().getActionCommand());
			int nr = Integer.parseInt(buttonGroup.getSelection().getActionCommand())-1;
			new Connect("servers.jks", "098765", false, false, true, false, null, nr).run();
		
			lblsub.setText("Oddałes głos");
			setVisible(false);
			panelMenu.setVisible(true);
		}
	};
}
